import sqlite3

import pandas as pd
import streamlit as st

import Config

conn = sqlite3.connect(Config.databases)
cursor = conn.cursor()
st.title('查看历史记录')

print('-' * 20, "查看历史", '-' * 20)


def paginate_dataframe(dataframe, page_size, page_num):
    offset = page_size * (page_num - 1)
    return dataframe.iloc[offset:offset + page_size]


# 执行查询
query = "SELECT * FROM history"
df = pd.read_sql_query(query, conn)

# 展示表格
# 用户输入每页显示的行数
page_size = st.sidebar.slider("每页显示行数", 1, 100, 20)

# 用户输入页码
page_num = st.sidebar.number_input("跳转到页码", min_value=1,
                                   max_value=len(df) // page_size + (1 if len(df) % page_size != 0 else 0),
                                   value=1, step=1)

# 分页显示DataFrame
paginated_df = paginate_dataframe(df, page_size, page_num)
st.dataframe(paginated_df)
x = st.number_input('输入需要展示的图片序号：', min_value=0, max_value=page_size)
if st.button('显示图片'):
    try:
        img_path = df.iloc[x, 1]
        st.image(img_path)
    except IndexError:
        st.markdown(f'<span style="color: red; font-size: 25px">输入数字错误</span>', unsafe_allow_html=True)
